﻿
using HDSmartCommunity.BUS;
using HDSmartCommunity.DAL;
using HDSmartCommunity.DevicePlatform;
using HDSmartCommunity.Models;
using HDSmartCommunity.Serial;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Data;

namespace HDSmartCommunity.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class FloorParameterController : ControllerBase
    {
        private readonly IHostingEnvironment _hostingEnvironment;
        public static DeviceSerialData serial = SerialServer.serial;

        public FloorParameterController(IHostingEnvironment hostingEnvironment)
        {
            _hostingEnvironment = hostingEnvironment;
        }

        /// <summary>
        /// 参数设置
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ParameterSet")]
        public ResponseMessage ParameterSet(int devid, string name, List<FloorAddress> list, string value)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;

                if (serial.SetParameter(devid, name, list, value))
                {
                    message.errcode = 0;
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = "参数设置失败";
                }
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "参数设置异常";
            }

            return message;
        }

        /// <summary>
        /// 参数读取
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [HttpGet]
        [Route("ParameterRead")]
        public ResponseMessage ParameterRead(int devid, string name)
        {

            ResponseMessage message = new ResponseMessage();
            try
            {
                DeviceSerialData serial = SerialServer.serial;
                List<FloorAddress> list = serial.ReadParameter(devid, name);

                message.errcode = 0;
                message.errmsg = "";
                message.data = list;
                message.total = list.Count;
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "参数读取异常";
            }

            return message;
        }

        /// <summary>
        /// 参数导出
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ParameterExport")]
        public ResponseMessage ParameterExport(List<FloorAddress> list, string name)
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ParamsBUS paramsBUS = new ParamsBUS();
                string sWebRootFolder = _hostingEnvironment.WebRootPath;
                message = paramsBUS.ExportParam(list, sWebRootFolder, name);
            }
            catch
            {
                message.errcode = 1;
                message.errmsg = "导出异常";
            }

            return message;
        }

        /// <summary>
        /// 参数导入
        /// </summary>
        /// <returns>success</returns>
        [HttpPost]
        [Route("ParameterImport")]
        [Obsolete]
        public ResponseMessage ParameterImport([FromForm] IFormCollection formCollection)
        {
            ResponseMessage message = new ResponseMessage();
            string filepath = string.Empty;
            string result = string.Empty;
            try
            {
                string webRootPath = _hostingEnvironment.ContentRootPath;
                ParamsBUS paramsBUS = new ParamsBUS();

                FormFileCollection filelist = (FormFileCollection)formCollection.Files;
                List<FloorAddress> list = new List<FloorAddress>();
                foreach (IFormFile file in filelist)
                {
                    filepath = paramsBUS.UploadFileToServer(file, webRootPath);
                    list = paramsBUS.GetFloorAddress(filepath);
                }

                message.errcode = 0;
                message.errmsg = "";
                message.data = list;
                message.total = list.Count;
            }
            catch (Exception e)
            {
                message.errcode = 1;
                message.errmsg = "导入参数异常";
                message.data = "";
                message.total = 0;
            }
            return message;
        }

        [HttpPost]
        [Route("GetParam")]
        public ResponseMessage GetParam()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                ParamDAL da = new ParamDAL();

                message.errcode = 0;
                message.data = da.GetParamType();
                message.total = 1;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "获取地址参数类型异常";
            }

            return message;
        }

        [HttpPost]
        [Route("GetBasicParam")]
        public ResponseMessage GetBasicParam()
        {
            ResponseMessage message = new ResponseMessage();
            try
            {
                DevParam devParam = new DevParam();
                ParamDAL da = new ParamDAL();
                devParam.mode = da.GetMode();
                devParam.version = serial.getversion();
                message.errcode = 0;
                message.data = devParam;
                message.total = 1;
            }
            catch (Exception ex)
            {
                message.errcode = 1;
                message.errmsg = "获取工作模式、版本信息异常";
            }

            return message;
        }
    }
}
